#include<iostream>
#include<unistd.h>
#include<stdlib.h>
#include<string.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<cstdio>
using namespace std;
#define PATH "./file.txt"
int main()
{
    umask(0);
    int fd = open(PATH,O_RDWR | O_CREAT | O_TRUNC,0644); //O_RDWR:读写方式打开
    if(fd < 0)
    {
        perror("open:");
        exit(1);
    }
    const char* str = "hello wolrd.......hello file.txt";
    ssize_t n = write(fd,str,strlen(str));//要写入的数据：str 期望写入的字节数：strlen(str) 不写入\0
    if(n != strlen(str))//返回值n：实际写入的字节个数
    {
        perror("write:");
        exit(2);
    }
    //注意：读取时要让文件指针回到开头---lseek
    lseek(fd,0,SEEK_SET); 
    char buff[1024] = {0};
    n = read(fd,buff,sizeof(buff)-1);
    if(n > 0)//返回值：实际读取的字节个数
    {
        buff[n] = '\0';
        cout << "read content:"<<buff<<endl;
    }
    close(fd);
    return 0;
}